var search;
search = new Vue({
    el: '#search',
    //初始化数据类型
    data: {
        type:0,
        goodsType: [],
        selected: [],
        warn: [],
        //所有商家信息
        Merchant:[],
        limit: [],
        //对比商品信息
        contrastInformation: [],
        ids:'',

    },
    //提交时间方法
    methods: {
        //鼠标事件
        over: function (id) {
            $("#" + id).css('display', 'block');
        },
        out: function (id) {
            $("#" + id).css('display', 'none');
        },
        //对比（弹出框）
        on_showClick: function (sire) {
            var _this = this;

            debugger
            var ID = sire.id;
            var T = true;
            var type = '';
            if (_this.contrastInformation != null) {
                for (var i = 0; i < _this.contrastInformation.length; i++) {
                    if (_this.contrastInformation[i].id == ID) {
                        _this.contrastInformation.splice(i, 1);
                        T = false;
                        sessionStorage.contrastInformation = JSON.stringify(_this.contrastInformation);
                    }
                    type = _this.contrastInformation[i].category;
                }
            }
            if (T) {
                if (_this.contrastInformation != null) {
                    if (_this.contrastInformation.length == 5) {
                        _this.$notify({
                            message: "请注意，最多选5个商品。",
                            type: 'error'
                        });
                        return;
                    } else {

                        if ((_this.contrastInformation.length > 0) && (type != sire.category)) {
                            _this.$notify({
                                message: "请注意，不同类型商品不允许比价。",
                                type: 'error'
                            });
                            return;
                        } else {
                            _this.contrastInformation.push(sire);
                            sessionStorage.contrastInformation = JSON.stringify(_this.contrastInformation);
                        }
                    }
                    $("#contrast").show();
                }else{
                    _this.contrastInformation.push(sire);
                    sessionStorage.contrastInformation = JSON.stringify(_this.contrastInformation);
                }
            }
        },
        on_hideClick: function () {
            var _this = this;
            debugger
            _this.contrastInformation = [];
            $(".close").click();
            $(".modal-backdrop.fade.in").remove();
            $(".on_show").removeAttr("checked");//取消
            $("#contrast").hide();
            // sessionStorage.clear();
            sessionStorage.contrastInformation=_this.contrastInformation;
        },
        on_shClick: function () {
            $("#dddddd").css('display', 'block');

        },
        onAddDataBefore:function (mixVal) {
            debugger
            var _this=this;
            for (var l = 0; l < _this.contrastInformation.length; l++) {
                if(l==0){
                    _this.ids=_this.contrastInformation[l].id;
                }else{
                    _this.ids=_this.ids+ ","+ _this.contrastInformation[l].id;
                }
            }
            _this.addShopCart(mixVal);
            _this.on_hideClick();
            _this.on_shClick();

        },
        //加入购物车
        addShopCart: function (lim) {
            var _this = this;
            if(_this.type==0){
                _this.$message({
                    message: '警告哦，您未登录！',
                    type: 'warning'
                });
            }else{
            for(var i=0;i<_this.Merchant.length;i++){
                if(lim.businessesId==_this.Merchant[i].id){
                    if(_this.Merchant[i].type==0){
                        _this.$message({
                            message: '警告哦，该商家未入围不能添加其商品至购物车！',
                            type: 'warning'
                        });
                    }else{
                        if(lim.install==0||lim.install==2){
                            _this.$message({
                                message: '警告哦，该商品已下架不能添加至购物车！',
                                type: 'warning'
                            });
                        }else{
                            $.post("/home/order_detail/insertShopCart",{"lim": JSON.stringify(lim),"sell":_this.Merchant[i].type,
                                "ids":_this.ids}, function (data) {
                                if(data.data.shopCart==1){
                                    _this.$message({
                                        message: '恭喜你，该商品已添加至购物车',
                                        type: 'success'
                                    });
                                }else{
                                    _this.$message({
                                        message: '警告哦，商品未添加至购物车',
                                        type: 'warning'
                                    });
                                }
                            });
                        }
                    }

                }
            }
            }


        },
        //
        seever:function(id){
            debugger
            $("#"+id).hide();
        },
        //跳转至商品详情页面
        Ontouch: function (lim) {
            $("#search").load("http://" + window.location.host + "/home/introduction.html", function () {
                introduction.product = lim;
            })
        },
        //商品搜索功能
        shopSelect: function (type) {
            var _this = this;
            $.post("/home/commodity/shopSelect", {"Name": $("#searchInput").val(), "type": 1}, function (data) {
                if(data.code==200){
                    _this.$message({
                        message: '恭喜你，查询成功！',
                        type: 'success'
                    });
                }else{
                    _this.$message({
                        message: '警告哦，查询失败！',
                        type: 'warning'
                    });
                }
                _this.limit = data.data.commodity;

            });
        },
        //搜索筛选功能
        comodity:function(number){
            var _this = this;
            $.post("/home/commodity/shopSelect",{"type":number},function(data){
                if(data.code==200){
                    _this.$message({
                        message: '恭喜你，成功查询到已入围商家的商品！',
                        type: 'success'
                    });
                }else{
                    _this.$message({
                        message: '警告哦，已入围商家的商品查询失败！',
                        type: 'warning'
                    });
                }
                _this.limit = data.data.commodityList;
            });

        },
        /*预警信息*/
        show_warn: function () {
            var _this = this;

            $.post("/home/order_detail/selectStatistics", "", function (data) {
                debugger;
                _this.warn = data.data.warnText;
            });
        },
        businesses:function(id){
            var _this = this;
        }

    },
    //页面加载后初始化方法
    created: function () {
        var _this = this;

        //初始化横向菜单列表
        var goodsTypeList = [
            {name: "询价表", href: "../home/offers.html"},
            {name: "报价单", href: "../home/quotation.html"},
        ];
        _this.goodsType = goodsTypeList
        //查看用户是否已经登录
        $.post("/home/dataCache/selectDataCache", "", function (data) {
            if(data.code == 200){
                _this.type = 1
            }

        });
        //商家信息
        $.post("/home/merchant/selectAll", "", function (data) {
            _this.Merchant = data.data.merchant;
        });

        var sessiones= sessionStorage.contrastInformation;
        if(sessiones==""||sessiones==null){

        }else{
            _this.contrastInformation = JSON.parse(sessiones);
            if (_this.contrastInformation !=null && _this.contrastInformation !='') {
                if (_this.contrastInformation.length > 0) {
                    $("#contrast").show();
                }
            }
        }


    },
});
